#include "connection.hpp"

void cb(const Channel::Ptr& channelPtr, const std::string& tag, const BasicProperty* bp, const std::string& body)
{
    std::cout << tag << "消费了：" << body << std::endl;
    // channelPtr->basicAck(bp->id());     //让服务端删掉对应的消息（内存，磁盘
}

int main(int argc, char* argv[])
{
    AsyncWorker::Ptr asyncWorkerPtr = std::make_shared<AsyncWorker>();
    Connection::Ptr connectionPtr = std::make_shared<Connection>("127.0.0.1", 8888, asyncWorkerPtr);

    Channel::Ptr channelPtr = connectionPtr->openChannel();
    google::protobuf::Map<std::string, std::string> empty_map;
    channelPtr->declareExchange("exchange1", ExchangeType::TOPIC, true, false, empty_map);
    channelPtr->declareMsgQueue("queue1", false, true, false, empty_map);
    channelPtr->declareMsgQueue("queue2", false, true, false, empty_map);
    channelPtr->bind("exchange1", "queue1", "news.sport");
    channelPtr->bind("exchange1", "queue2", "news.music.#");

    channelPtr->basicConsumer("consumer1", argv[1], true, std::bind(cb, channelPtr, _1, _2, _3));      //这个只不过是告诉服务器多了一个消费者，它没有主动接收的功能
                                                                                                        //只有publish才会推送消息的时候，才会顺便给消费者推送
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));

    connectionPtr->closeChannel(channelPtr);        //关闭信道
    return 0;
}